package com.aerotechnic.dao.implementation;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.aerotechnic.dao.interfaces.JalonDaoIn;
import com.aerotechnic.entities.DRF;
import com.aerotechnic.entities.Jalon;

@Repository("JalonDao")
public class JalonDao implements JalonDaoIn {

	@Autowired
	private SessionFactory sessionFactory;

	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	@Override
	public void save(Jalon t) {

		// TODO Auto-generated method stub

		getCurrentSession().save(t);

	}

	@Override
	public void delete(Long id) {
		// TODO Auto-generated method stub

		Jalon e = findByID(id);
		if (e != null)
			getCurrentSession().delete(e);

	}

	@Override
	public Jalon findByID(Long id) {
		// TODO Auto-generated method stub

		Session session = sessionFactory.getCurrentSession();

		return (Jalon) session.createQuery("FROM Jalon where id= " + id).list()
				.get(0);
	}

	@Override
	public void update(Jalon t) {
		// TODO Auto-generated method stub

		getCurrentSession().update(t);

	}

	public List<Jalon> findAll() {
		// TODO Auto-generated method stub

		return getCurrentSession().createQuery("from Jalon").list();
	}

	@Override
	public List<Jalon> find(int jtStartIndex, int jtPageSize) {
		// TODO Auto-generated method stub
		return getCurrentSession().createQuery("from Jalon")
				.setFirstResult(jtStartIndex).setMaxResults(jtPageSize).list();
	}

}
